function formatNumber(number, length) {
  return number.toString().padStart(length, '0')
}

export function formatDate(date, pattern) {
  if (!date) date = new Date()
  if (typeof date === 'number') date = new Date(date)
  if (!pattern) pattern = 'yyyy-MM-dd HH:mm:ss'

  const year = formatNumber(date.getFullYear(), 4)
  const month = formatNumber(date.getMonth() + 1, 2)
  const day = formatNumber(date.getDate(), 2)
  const hour = formatNumber(date.getHours(), 2)
  const minute = formatNumber(date.getMinutes(), 2)
  const second = formatNumber(date.getSeconds(), 2)

  return pattern
    .replace('yyyy', year)
    .replace('MM', month)
    .replace('dd', day)
    .replace('HH', hour)
    .replace('mm', minute)
    .replace('ss', second)
}
